﻿using Cxuu.Common;
using Cxuu.Models;
using Cxuu.Repository;
using PetaPoco;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Cxuu.Service
{
    [AppService]
    public class ArticleService
    {
        private readonly ArticleRepository articleDal;
        private readonly ArticleContentRepository articleContentDal;
        private readonly ICacheManager _cacheManager;
        private readonly string cacheKey;

        public ArticleService(ArticleRepository articleRepository, ArticleContentRepository contentRepository, ICacheManager cacheManager)
        {
            articleDal = articleRepository;
            articleContentDal = contentRepository;
            _cacheManager = cacheManager;
            cacheKey = CacheConst.CACHE_KEY_COUNTARTICLE;
        }

        /// <summary>
        /// 查询一条自己的最新信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Article> GetByUidAsync(int userId,int cid)
        {
            return await articleDal.GetByUidAsync(userId,cid);
        }

        /// <summary>
        /// 后台编辑一条信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<EditArticleAndContent> GetEditAsync(int? id)
        {
            return await articleDal.GetEditAsync(id);
        }
        /// <summary>
        /// 自动推断新建文号
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        public async Task<int> GetArticleByCidAsync(int cid)
        {
            Article article = await articleDal.GetArticleByCidAsync(cid);
            if (article != null && DateTime.Compare(article.InsertTime, new(DateTime.Now.Year, 1, 1)) > 0)
                return article.DocNumber + 1;
            else
                return 1;
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="articleQuery"></param>
        /// <returns></returns>
        public async Task<Page<ViewArticleAndCate>> GetListByCidAdminAsync(ArticleQuery articleQuery)
        {
            articleQuery.Orderby = "Id desc";
            return await articleDal.GetListAsync(articleQuery);
        }


        /// <summary>
        /// 写稿一条新的内容
        /// </summary>
        /// <param name="article"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public virtual async Task<Article> InsertArticleAndContentAsync(Article article, ArticleContent content)
        {
            article.DocNumber = await GetArticleByCidAsync(article.Cid);//自动生成文号
            Article articleRes = await articleDal.InsertArticleAndContentAsync(article, content);
            return articleRes;
        }
        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="article"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public virtual async Task<int> UpdateArticleAndContentAsync(Article article, ArticleContent content)
        {
            return await articleDal.UpdateArticleAndContentAsync(article, content);
        }


        /// <summary>
        /// 删除一条信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual async Task<int> DelArticleAndContentAsync(int id)
        {
            if (await articleDal.DeleteByPriKeyAsync(id) > 0)
            {
                await articleContentDal.DeleteByPriKeyAsync(id);
                return 1;
            }
            return 0;
        }

        public async Task<List<ArticleCount>> CountArticleForMonthAsync()
        {
            string cacheName = cacheKey + "ArticleForMonth";
            List<ArticleCount> count = _cacheManager.Get<List<ArticleCount>>(cacheName);
            if (count == null)
            {
                count = await articleDal.CountArticleForMonthAsync();
                _cacheManager.Set(cacheName, count, TimeSpan.FromMinutes(11));
            }
            return count;
        }

        public async Task<List<ArticleCount>> CountArticleForUserAsync(bool isMonth = true)
        {
            if (isMonth)
            {
                string cacheName = cacheKey + "CountArticleForUserMonthAsync";
                List<ArticleCount> count = _cacheManager.Get<List<ArticleCount>>(cacheName);
                if (count == null)
                {
                    count = await articleDal.CountArticleForUserAsync(true);
                    _cacheManager.Set(cacheName, count, TimeSpan.FromMinutes(8));
                }
                return count;
            }
            else
            {
                string cacheName = cacheKey + "CountArticleForUserYearAsync";
                List<ArticleCount> count = _cacheManager.Get<List<ArticleCount>>(cacheName);
                if (count == null)
                {
                    count = await articleDal.CountArticleForUserAsync(false);
                    _cacheManager.Set(cacheName, count, TimeSpan.FromMinutes(6));
                }
                return count;
            }

        }
        public async Task<List<ArticleCount>> CountUserGroupYearAsync()
        {
            string cacheName = cacheKey + "CountUserGroupYearAsync";
            List<ArticleCount> count = _cacheManager.Get<List<ArticleCount>>(cacheName);
            if (count == null)
            {
                count = await articleDal.CountUserGroupYearAsync();
                _cacheManager.Set(cacheName, count, TimeSpan.FromMinutes(5));
            }
            return count;
        }

        public async Task<NumCount> CountAllAsync()
        {
            string cacheName = cacheKey + "CountAll";
            NumCount count = _cacheManager.Get<NumCount>(cacheName);
            if (count == null)
            {
                count = await articleDal.CountAllAsync();
                _cacheManager.Set(cacheName, count, TimeSpan.FromMinutes(10));
            }
            return count;
        }


        /// <summary>
        /// 统计查询
        /// </summary>
        /// <param name="articleQuery"></param>
        /// <returns></returns>
        public async Task<List<Statistics>> StatisticsAsync(StatisticsQuery articleQuery)
        {
            return await articleDal.StatisticsAsync(articleQuery);
        }
    }
}
